<?php
// +----------------------------------------------------------------------
// | 海豚PHP框架 [ DolphinPHP ]
// +----------------------------------------------------------------------
// | 版权所有 2016~2017 河源市卓锐科技有限公司 [ http://www.zrthink.com ]
// +----------------------------------------------------------------------
// | 官方网站: http://dolphinphp.com
// +----------------------------------------------------------------------
// | 开源协议 ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------

namespace app\warehouse\model;

use think\Model;

/**
 * 运费配置模型
 * @package app\warehouse\model
 */
class Freight extends Model
{
    // 设置当前模型对应的完整数据表名称
    protected $table = '__WAREHOUSE_FREIGHT__';

    // 自动写入时间戳
    protected $autoWriteTimestamp = true;

    /**
     * 获取所有运费配置
     * @param array $map 条件
     * @param string $order 排序
     * @author 陈梦晨 <2391157750@qq.com>
     * @return mixed
     */
    public static function getAll($map = [], $order = '')
    {   
        $data_list = self::view('warehouse_freight', true)
            ->view('warehouse_list', 'warehousename', 'warehouse_freight.warehouseid=warehouse_list.warehouseid', 'left')
            ->view('admin_carrier', 'carriername', 'warehouse_freight.carrierid=admin_carrier.carrierid', 'left')
            ->view('admin_distribute', ['title'=>'distributename'], 'warehouse_freight.distributeid=admin_distribute.id', 'left')
            ->where($map)
            ->order($order)
            ->paginate();
        return $data_list;
    }
    /**
     * 启用
     * @param string
     * @author 陈梦晨 <2391157750@qq.com>
     * @return bool
     */
    public static function enable($ids = '')
    {
        foreach($ids as $id){
            $result[]=self::where('id', $id)->setField('status', 1);
        }
        return $result;
    }

    /**
     * 禁用
     * @param string
     * @author 陈梦晨 <2391157750@qq.com>
     * @return bool
     */
    public static function disable($ids = '')
    {
        foreach($ids as $id){
            $result[]=self::where('id', $id)->setField('status', 0);
        }
        return $result;
        
    }

    /**
     * 获取一个物流费用配置项
     * @param int $warehouseId      仓库
     * @param array $orderAddress   订单收货地址
     * @param int $carrierId        承运商ID
     * @param int $logisticsId      物流方式ID
     * @param int $weight           重量 克
     * @author sky
     * @return float
     */
    public function getFeightConfig($warehouseId, $orderAddress=array(), $carrierId, $logisticsId)
    {
        $map['warehouseid'] = $warehouseId;
        $map['provinceid'] = $orderAddress['provinceid'];
        $map['cityid'] = $orderAddress['cityid'];
        $map['townid'] = $orderAddress['townid'];
        $map['carrierid'] = $carrierId;
        $map['distributeid'] = $logisticsId;
        $map['status'] = 1 ;
        $feight = $this->where($map)->field('ykg,ykgfee,continued,continuedfee,aging')->find();
        //dump($feight);exit;
        $data = $feight ? $feight->getData() : '';
        return $data;
    }

    /**
     * 计算订单物流费用
     * @param array $feightConfig   订单收货地址
     * @param int $weight           重量 克
     * @author sky
     * @return float
     */
    public function getFeightCost($feightConfig, $weight)
    {
        //默认运费为0
        $cost = 0;

        $row = $feightConfig;

        //如果没有设置运费，返回默认运费
        if(empty($row)) return $cost;
        
        if($row['ykg']>=$weight){
            $cost = $row['ykgfee'];
        }else{
            $xz = round(($weight-$row['ykg'])/$row['continued'],2); //续重
            $cost = $row['ykgfee'] + ceil($xz)*$row['continuedfee']; //超重运费 = 首重费+续重费
        }

        return $cost;
    }

}